import axios, { AxiosRequestConfig, AxiosResponse } from "axios"
import { FileUtils } from "./fileutils"
import { UploadUtils } from "./TCDNUploadFlowPorts";


export class WechatCI{

    static readonly DEFAULT_GROUP_ID = "2863587";

    static async robot(qrUrl:string){
        const utils = UploadUtils
        let curVersion = await utils.getCurVersion();
        console.log("qr###",qrUrl);
        this.robotMsg(WechatCI.DEFAULT_GROUP_ID,`版本:${curVersion},二维码:[img]${qrUrl}[/img]`)
    }

    static robotMsg(groupId:string,msg:string){
        console.log("groupId ",groupId,"msg ",msg);
        
        axios.post('https://open.popo.netease.com/open-apis/token',{
            appId: "", appSecret: ""
        })
        .then(function(response:AxiosResponse) {
            //console.log(response);
            let resData = response.data; 
            let token = resData.data.openAccessToken
            
            const data ={
                "sender": "",
                "receiver":groupId,
                "message":msg
            }

            let options:AxiosRequestConfig = {
                method:"POST",
                headers:{"Content-Type": "application/json","Open-Access-Token": token},
                data:JSON.stringify(data),
                url:"https://open.popo.netease.com/open-apis/robot/v1/team"
            };

            axios(options)
            .then(function(resp){
              //  console.log(resp);
            })
            .catch(function(err){
                console.log(err)
            });
        })
        .catch(function(err){
            console.log(err);
        })
    }

    static async saveBuildInfo(msg:string,platform:string = "wechat"){
        if(platform == "wechat"){

            await FileUtils.wrapFileContent({
                fileGlob: process.cwd()+"/../client/build/wechatgame/src/assets/script/deps/version*.js",
                wrapContent: "${rawcontent}" +	
                `(function () {
                    var Version = window["Version"];
                    Version.BUILD_INFO = "${msg}"
                })();`
            })
        }else if(platform == "web-mobile"){
            await FileUtils.wrapFileContent({
                fileGlob: process.cwd()+"/../client/build/web-mobile/src/assets/script/deps/version*.js",
                wrapContent: "${rawcontent}" +	
                `(function () {
                    var Version = window["Version"];
                    Version.BUILD_INFO = "${msg}"
                })();`
            })
        }
    }

}